package com.example.concurrentprogramming8016.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-03 16:31
 *
 */
public class VolatileDemo {
    private static volatile boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("trun on");
            flag = false;
        });
        Thread thread1 = new Thread(() -> {
            while (true) {
                if (!flag) {
                    System.out.println("turn off");
                    flag = true;
                    break;
                }
                System.out.println("gogogo");
            }
        });
        thread1.start();
        TimeUnit.SECONDS.sleep(1);
        thread.start();

    }
}
